﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Controls
{
    public partial class ScrollBar : UserControl
    {
        PointF p;
        Font f = new Font("宋体", 10);
        Color c = Color.White;
        string temp;

        private string _scrollText;
        /// <summary>
        /// 设置滚动字幕
        /// </summary>
        public string ScrollText
        {
            get { return _scrollText; }
            set { _scrollText = value; }
        }

        private bool _isSroll;

        public bool IsSroll
        {
            get { return _isSroll; }
            set { _isSroll = value; }
        }

        public ScrollBar()
        {
            InitializeComponent();

            Init();
        }

        void Init()
        {
            //if (IsSroll)
            {
                this.lbText.Text = "";
                this.timer1.Enabled = true;
                this.timer1.Interval = 500;
                p = new PointF(this.lbText.Size.Width, 0);
            }
            //else
            //{
            //    this.timer1.Enabled = false;
            //    this.lbText.Text = ScrollText;
            //}
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.lbText.Font = this.Font;
            this.lbText.BackColor = this.BackColor;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ScrollText))
            {
                this.timer1.Enabled = false;
            }

            Graphics g = this.lbText.CreateGraphics();
            SizeF s = new SizeF();
            s = g.MeasureString(ScrollText, f);//测量文字长度
            Brush brush = Brushes.Black;
            g.Clear(c);//清除背景

            if (temp != ScrollText)//文字改变时,重新显示
            {
                p = new PointF(this.lbText.Size.Width, 0);
                temp = ScrollText;
            }
            else
                p = new PointF(p.X - 10, 0);//每次偏移10
            if (p.X <= -s.Width)
                p = new PointF(this.lbText.Size.Width, 0);
            g.DrawString(ScrollText, f, brush, p);
        }
    }
}
